import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description
 * User:user
 * Date:2021-10-16
 * Time:9:39
 */
public class TestDemo {

/*    public  static 返回值 方法名称（参数类型 参数个数）{
        方法的主体
    }
    main(){
        方法名称(实际参数)
    }
    匹配：1.函数的参数：个数 类型
            2.返回值要对应起来*/
    //import java.util.*;


/*    public static void bubbleSort(int[] arry){
        for (int i = 0; i < arry.length - 1; i++) {
            int flag = 1;
            for (int j = 0; j < arry.length - 1 - i; j++) {
                if(arry[j] > arry[j+1]){
                    int tmp = arry[j];
                    arry[j] = arry[j+1];
                    arry[j+1] = tmp;
                    flag = 0;
                }
            }
            if(flag == 1){
                System.out.println(i+"排好序");
                break;
            }
        }
    }
    public static void main(String[] args) {
        int[] arry = {19,12,3,41,5,6};
        bubbleSort(arry);
        System.out.println(Arrays.toString(arry));
    }*/

//    //检查数组是否有序
//    public static boolean isSorted(int[] arry){
          //注意最后一个元素的下标
//        for (int i = 0; i < arry.length - 1; i++) {
//            if(arry[i] > arry[i+1]){
//                return false;
//            }
//        }
//        return false;
//    }
//    public static void main(String[] args) {
//        int[] arry={4,5,7,2,1};
//        System.out.println(isSorted(arry));
//    }






/*
    public static int binarySearch(int[] arry,int key){
        int left = 0;
        int right = arry.length - 1;
        while(left<=right){
            //mid求值必须放到循环里面，因为是不断更新的
            int mid = (left+right);
            if(arry[mid] > key){
                right = mid - 1;
            }else if(arry[mid] < key){
                left = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] arry ={4,9,18,65,45,23};
        //二分查找必须是初始数组有序，可以采用sort进行排序
        Arrays.sort(arry);//进行从小到大排序
        System.out.println(Arrays.binarySearch(arry, 45));
        System.out.println(binarySearch(arry,23));

    }*/



/*
    //二分查找  折半查找  在有序数组中进行查找
    public static int binarySearch(int[] arry,int key){
        int left = 0;
        int right = arry.length - 1;
        while(left<=right){
            //mid求值必须放到循环里面，因为是不断更新的
            int mid = (left+right)/2;
            if(arry[mid] > key){
                right = mid - 1;
            }else if(arry[mid] < key){
                left = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(binarySearch(arry,4));
    }
*/

/*

    //顺序查找数据元素,时间复杂度为o(n)
    public  static  int get(int[] arry,int key){
        for (int i = 0; i < arry.length; i++) {
            if(arry[i] == key){
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] arry = {1,2,3,7,8};
        //get(arry,1).sout  快捷输入，即可得到下一行代码
        System.out.println(get(arry,8));
    }
*/









/*
    //求出数组元素的平均值
    public static double avg(int[] arry){
        double sum = 0.0;
        for (int i = 0; i < arry.length; i++) {
            sum += arry[i];
        }
        //增强for循环的使用
        for(int value:arry){
            sum += value;
        }
        return sum/arry.length;
    }
    //求出两数的最大值
    public  static int max(int[] arry){
        int maxNum = arry[0];
        for (int i = 1; i < arry.length; i++) {
            if(arry[i]  > maxNum){
                maxNum = arry[i];
            }
        }
        return maxNum;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,45};
        System.out.println(max(arry));
        System.out.println(avg(arry));
    }*/

/*
    //数组转为字符串,模拟实现Arrays.toString方法
    public static  String  myToString(int[] arry){
        if(arry == null)return "null";
        if(arry.length == 0)return "[]";
        String ret = "[";
        for (int i = 0; i < arry.length; i++) {
            ret += arry[i];
            if(i < arry.length - 1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(myToString(arry));
//        int[] arry;
//        System.out.println(myToString(arry));//会报错空指针异常
//        int[] arry == null;
//        System.out.println(myToString(arry));
          String s1 = null;
          System.out.println(s1.length());//空指针异常
          String s2 = "";
          System.out.println(s1.length());//可以求出数组长度为0
    }
*/






/*
    //数组作为函数参数的返回值
    public static int[] func5(int[] arry){
        int[] ret = new int[arry.length];
        for (int i = 0; i < arry.length; i++) {
                ret[i] = arry[i] * 2;
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4};
        int[] v = func5(arry);
        System.out.println(Arrays.toString(v));
    }*/



 /*   //返回数组也不一定需要，效果完全可以实现
    public static int[] func4(int[] arry){
        for (int i = 0; i < arry.length; i++) {
            arry[i] *= 2;
        }
        return arry;
    }

    //将一个数组里的所有元素都扩大二倍
    public static void fun3(int[] arry){
        for (int i = 0; i < arry.length; i++) {
            arry[i] *= 2;
        }
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        fun3(arry);
        System.out.println(Arrays.toString(arry));
        int[] ret = func4(arry);
        System.out.println(Arrays.toString(ret));
    }*/


/*
    public static void func1(int[] arry){
        arry = new int[10];

    }
    public static void func2(int[] arry){
        arry[0] = 9;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        func1(arry);
        System.out.println(Arrays.toString(arry));
        func2(arry);
        System.out.println(Arrays.toString(arry));

    }
*/



   /* public static void bubbleSort(int[] arry){
        for (int i = 0; i < arry.length - 1; i++) {
            int flag = 0;
            for (int j = 0; j < arry.length - 1 - i; j++) {
                if(arry[j] > arry[j+1]){
                    int tmp = arry[j];
                    arry[j] = arry[j + 1];
                    arry[j + 1] = tmp;
                    flag = 1;
                }
            }
            if(flag==0){
                System.out.println("第"+i+"躺已经有序");
                break;
            }
        }
    }
    public static void main(String[] args) {
        int[] arry = {1,5,9,6,8,7};
        bubbleSort(arry);
        System.out.println(Arrays.toString(arry));
    }
*/
/*
    public static boolean func(int[] arry){
        for (int i = 0; i < arry.length - 1; i++) {
            if(arry[i] > arry[i+1]){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(func(arry));
    }*/
/*

    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(array[mid]> key){
                right = mid - 1;
            }else if(array[mid] == key){
                return mid;
            }else{
                left = left +1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,5,6,7,9};
        System.out.println(binarySearch(arry,9));
    }
*/
/*
   public static String myToString(int[] arry){
        String ret = "[";
        for (int i = 0; i < arry.length; i++) {
            ret += arry[i];
            if(i < arry.length - 1){
                ret +=  ",";
            }
        }
        ret += "]";
        return ret;
    }

    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(myToString(arry));
    }*/
/*
    public static int maxNum(int[] arry){
        int max = arry[0];
        for (int i = 1; i < arry.length; i++) {
            if(arry[i] > max){
                max = arry[i];
            }
        }
        return max;
    }
    public static double avg(int[] arry){
        int sum = 0;
        double avg = 0.0;
        for (int i = 0; i < arry.length; i++) {
            sum += arry[i];
        }
        avg = sum / arry.length;
        return avg;

    }
    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        System.out.println(maxNum(arry));
        System.out.println(avg(arry));
    }*/



/*    public static String[] myToString(int[] arry)
    {
        String[] ret = new String[arry.length];
        ret[0] = "["+arry[0]+",";
        for (int i = 1; i < arry.length - 1; i++) {
            ret[i] = arry[i] + ",";
        }
        ret[arry.length - 1] = arry[arry.length - 1] + "]";
        return ret;
    }
    public static void main(String[] args) {
        int[] arry = {1,2,3,4,5};
        String[] str = myToString(arry);
        for (int i = 0; i < arry.length; i++) {
            System.out.print(str[i]);
        }
    }*/



}
